package com.budwk.app.sqfwV2.enums.povertyMonitoring;

import com.budwk.app.sqfwV2.dto.povertyMonitoring.QueryTimeBuildDTO;
import lombok.Getter;

import java.time.YearMonth;
import java.util.*;

@Getter
public enum QuarterEnum {
    //目前先用枚举是季度
    ONE("第一季度", "10", "12", 1,-1),
    TOW("第二季度", "01", "03", 2,0),
    THREE("第三季度", "04", "06", 3, 0),
    FOUR("第四季度", "07", "09", 4, 0),

    ;

    private final String name;

    private final String startTime;//开始月份（包含）

    private final String endTime;//结束月份（包含）

    private final int num;

    private final int straddleYear;//是否跨年,-1-上一年，0-当前年，1-下一年


    QuarterEnum(String name, String startTime, String endTime, int num,int straddleYear) {
        this.name = name;
        this.startTime = startTime;
        this.endTime = endTime;
        this.num = num;
        this.straddleYear=straddleYear;
    }

    public static QuarterEnum getQuarterEnumByMonth(Integer month) {
        if (null == month) {
            Calendar calendar = Calendar.getInstance();
            month = calendar.get(Calendar.MONTH) + 1;
        }
        for (QuarterEnum item : QuarterEnum.values()) {
            int startTime = Integer.parseInt(item.getStartTime());
            int endTime = Integer.parseInt(item.getEndTime());
            if (startTime > endTime) {
                //跨年
                if (startTime <= month || endTime >= month) {
                    return item;
                }
            } else {
                if (startTime <= month && endTime >= month) {
                    return item;
                }
            }
        }
        return null;
    }

    public static QueryTimeBuildDTO getQuarterEnumAndSetTimeByMonth(QuarterEnum quarterEnum) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String startTimeSet = null;
        String endTimeSet = null;
        int startTime = Integer.parseInt(quarterEnum.getStartTime());
        int endTime = Integer.parseInt(quarterEnum.getEndTime());
        if (startTime > endTime) {
            //跨年
            if (startTime <= month) {
                startTimeSet = String.format("%s%s", year, quarterEnum.getStartTime());
                endTimeSet = String.format("%s%s", year + 1, quarterEnum.getEndTime());
            } else {
                startTimeSet = String.format("%s%s", year - 1, quarterEnum.getStartTime());
                endTimeSet = String.format("%s%s", year, quarterEnum.getEndTime());
            }
        } else {
            startTimeSet = String.format("%s%s", year, quarterEnum.getStartTime());
            endTimeSet = String.format("%s%s", year, quarterEnum.getEndTime());
        }

        QueryTimeBuildDTO queryTimeBuildDTO = new QueryTimeBuildDTO();
        queryTimeBuildDTO.setStartTimeNum(Integer.parseInt(startTimeSet));
        queryTimeBuildDTO.setEndTimeNum(Integer.parseInt(endTimeSet));

        return queryTimeBuildDTO;
    }

    public static List<Map<String, Object>> getQuarterList() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        String startTime = QuarterEnum.ONE.startTime;
        int num = QuarterEnum.ONE.getStraddleYear();
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        for (QuarterEnum quarterEnum : QuarterEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", quarterEnum.getName());
            map.put("startTime", startTime);
            map.put("endTime", quarterEnum.getEndTime());
            map.put("num", quarterEnum.getNum());
            map.put("left", num);
            int startTimeQ = Integer.parseInt(quarterEnum.getStartTime());
            int endTimeQ = Integer.parseInt(quarterEnum.getEndTime());

            if (startTimeQ > endTimeQ) {
                map.put("right", num+1);
                //跨年
                if (startTimeQ <= month || endTimeQ >= month) {
                    map.put("select", true);
                }else{
                    map.put("select", false);
                }
            } else {
                map.put("right", quarterEnum.getStraddleYear());
                if (startTimeQ <= month && endTimeQ >= month) {
                    map.put("select", true);
                }else{
                    map.put("select", false);
                }
            }
            mapList.add(map);
        }
        return mapList;
    }


    public static QuarterEnum getQuarterEnumByNum(Object quarter) {
        for(QuarterEnum quarterEnum:QuarterEnum.values()){
            if(String.valueOf(quarterEnum.num).equals(quarter)){
                return quarterEnum;
            }
        }
        return null;
    }
}
